import 'package:casdon/modules/lifeCircle/data/api/life_topic_comment_list_api.dart';

class LifeTopicCommentViewData {
  final String id;
  final String topicId;
  final String userId;
  final String userName;
  final String avatarUrl;
  final String content;
  final String displayTime;
  final int likeCount;
  final bool isLiked;
  final bool isOfficial;
  final bool isTopicOwner;
  final bool hasBrandReply;
  final bool isMine;
  final String? region;
  final List<String> imageUrls;
  final List<LifeTopicCommentViewData> replies;
  final int replyCount;
  final bool isMain;

  const LifeTopicCommentViewData({
    required this.id,
    required this.topicId,
    required this.userId,
    required this.userName,
    required this.avatarUrl,
    required this.content,
    required this.displayTime,
    required this.likeCount,
    required this.isLiked,
    required this.isOfficial,
    required this.isTopicOwner,
    required this.hasBrandReply,
    required this.isMine,
    required this.region,
    required this.imageUrls,
    required this.replies,
    required this.replyCount,
    required this.isMain,
  });

  LifeTopicCommentViewData copyWith({
    String? id,
    String? topicId,
    String? userId,
    String? userName,
    String? avatarUrl,
    String? content,
    String? displayTime,
    int? likeCount,
    bool? isLiked,
    bool? isOfficial,
    bool? isTopicOwner,
    bool? hasBrandReply,
    bool? isMine,
    String? region,
    List<String>? imageUrls,
    List<LifeTopicCommentViewData>? replies,
    int? replyCount,
    bool? isMain,
  }) {
    return LifeTopicCommentViewData(
      id: id ?? this.id,
      topicId: topicId ?? this.topicId,
      userId: userId ?? this.userId,
      userName: userName ?? this.userName,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      content: content ?? this.content,
      displayTime: displayTime ?? this.displayTime,
      likeCount: likeCount ?? this.likeCount,
      isLiked: isLiked ?? this.isLiked,
      isOfficial: isOfficial ?? this.isOfficial,
      isTopicOwner: isTopicOwner ?? this.isTopicOwner,
      hasBrandReply: hasBrandReply ?? this.hasBrandReply,
      isMine: isMine ?? this.isMine,
      region: region ?? this.region,
      imageUrls: imageUrls ?? this.imageUrls,
      replies: replies ?? this.replies,
      replyCount: replyCount ?? this.replyCount,
      isMain: isMain ?? this.isMain,
    );
  }

  factory LifeTopicCommentViewData.fromApi(TopicComment comment) {
    final imageSrc = comment.imageSrc ?? '';
    final imageList = imageSrc.isEmpty
        ? <String>[]
        : imageSrc
              .split(',')
              .map((e) => e.trim())
              .where((e) => e.isNotEmpty)
              .toList();

    final children = (comment.children ?? const [])
        .map(LifeTopicCommentViewData.fromApi)
        .toList();

    int _countDescendants(List<LifeTopicCommentViewData> list) {
      var total = list.length;
      for (final child in list) {
        total += _countDescendants(child.replies);
      }
      return total;
    }

    return LifeTopicCommentViewData(
      id: comment.id,
      topicId: comment.topicId,
      userId: comment.userId,
      userName: (comment.nickName ?? '').isEmpty ? '匿名用户' : comment.nickName!,
      avatarUrl: comment.userAvatar ?? '',
      content: comment.content ?? '',
      displayTime: _formatTimestamp(comment.createTime),
      likeCount: comment.likes ?? 0,
      isLiked: comment.isLike ?? false,
      isOfficial: comment.isOfficial ?? false,
      isTopicOwner: comment.isLz ?? false,
      hasBrandReply: comment.reply ?? false,
      isMine: comment.isMyComment ?? false,
      region: comment.region,
      imageUrls: imageList,
      replies: children,
      replyCount: _countDescendants(children),
      isMain: comment.isMain ?? false,
    );
  }

  static String _formatTimestamp(String? timestamp) {
    if (timestamp == null || timestamp.isEmpty) return '';
    DateTime? dateTime = DateTime.tryParse(timestamp);
    dateTime ??= DateTime.tryParse(timestamp.replaceFirst(' ', 'T'));
    if (dateTime == null) return timestamp;
    final month = dateTime.month.toString().padLeft(2, '0');
    final day = dateTime.day.toString().padLeft(2, '0');
    final hour = dateTime.hour.toString().padLeft(2, '0');
    final minute = dateTime.minute.toString().padLeft(2, '0');
    return '$month-$day $hour:$minute';
  }
}

class LifeTopicCommentListState {
  final List<LifeTopicCommentViewData> comments;
  final int totalSize;
  final int page;
  final int pageSize;
  final bool isLoadingMore;
  final bool hasMore;

  const LifeTopicCommentListState({
    required this.comments,
    required this.totalSize,
    required this.page,
    required this.pageSize,
    required this.isLoadingMore,
    required this.hasMore,
  });

  factory LifeTopicCommentListState.initial() => LifeTopicCommentListState(
    comments: const [],
    totalSize: 0,
    page: 0,
    pageSize: 10,
    isLoadingMore: false,
    hasMore: false,
  );

  LifeTopicCommentListState copyWith({
    List<LifeTopicCommentViewData>? comments,
    int? totalSize,
    int? page,
    int? pageSize,
    bool? isLoadingMore,
    bool? hasMore,
  }) {
    return LifeTopicCommentListState(
      comments: comments ?? this.comments,
      totalSize: totalSize ?? this.totalSize,
      page: page ?? this.page,
      pageSize: pageSize ?? this.pageSize,
      isLoadingMore: isLoadingMore ?? this.isLoadingMore,
      hasMore: hasMore ?? this.hasMore,
    );
  }
}
